<?php

namespace app\admin\controller;


use app\common\controller\CommonController;
use app\common\service\AuthService;
use app\common\tools\Data;
use think\Db;

class User extends CommonController
{

    public $table = 'SystemUser';
    public function index()
    {

        $this->title = '系统用户管理';
        $list = Db::name('SystemUser')->where(['is_deleted' => '0'])->order('id desc')->paginate(10);

        $page = $list->render();
        $this->assign('list',$list);
        $this->assign('page', $page);
        $this->assign('title',$this->title);
        return $this->fetch();
    }

    /**
     * 编辑系统用户
     * @return mixed
     */
    public function edit()
    {
        if(request()->isPost()){
            $id = input('post.id');
            $phone = input('post.phone');
            $mail = input('post.mail');
            $desc = input('post.desc');
            $updateData['phone'] = $phone;
            $updateData['mail'] = $mail;
            $updateData['desc'] = $desc;
            $authorize = input('post.authorize/a');
            $updateData['authorize'] = join(',',$authorize);
            $res = Db::name('SystemUser')->where(['id'=>$id])->limit(1)->update($updateData);
            if($res){
                $this->_success('编辑成功');
            }else{
                $this->_error('编辑失败');
            }

        }
        $id =  input('get.id');
        if($id){
            $find = Db::name('SystemUser')->where(['id'=>$id])->find();
            if($find){
                $find['authorize'] = explode(',',$find['authorize']);
            }
        }
        $authorizes = Db::name('SystemAuth')->where(['status' => '1'])->select();
        $this->assign('authorizes', $authorizes);
        $this->assign('vo',$find);
        return $this->fetch('form');
    }
    /**
     * 修改用户密码
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function pass()
    {
        $id = input('get.id');

        if (request()->isPost()) {
            $post = $this->request->post();
            if ($post['password'] !== $post['repassword']) {
                $this->error('两次输入的密码不一致！');
            }
            $result = AuthService::checkPassword($post['password']);
            if (empty($result['code'])) $this->error($result['msg']);
            $data = ['id' => $post['id'], 'password' => md5($post['password'])];
            $res = Db::name('SystemUser')->where(['id'=>$id])->update($data);
            if ($res) {
                $this->_success('密码修改成功，下次请使用新密码登录！', '');
            } else {
                $this->_error('密码修改失败，请稍候再试！');
            }
        }

        $find = Db::name('SystemUser')->where(['id'=>$id])->find();
        $this->assign('vo',$find);
        $this->assign('title','修改密码');
        return $this->fetch();
    }

    /**
     * 用户授权管理
     * @return mixed
     */
    public function auth()
    {
        $id = input('get.id');
        if(request()->isPost()){
            $authorize = input('post.authorize/a');
            $id = input('post.id');
            $postData['authorize'] = join(',',$authorize);
            $postData['id'] = $id;
            $res = Db::name($this->table)->where(['id'=>$id])->update($postData);
            if($res){
                $this->_success('用户授权成功');
            }else{
                $this->_error('用户授权失败');
            }
        }
        $find = Db::name($this->table)->where(['id'=>$id])->find();
        if($find){
            $find['authorize'] = explode(',',$find['authorize']);
        }
        $authorizes = Db::name('SystemAuth')->where(['status' => '1'])->select();
        $this->assign('vo',$find);
        $this->assign('authorizes', $authorizes);
        $this->assign('title','用户授权');
        return $this->fetch();
    }

    /**
     * 删除系统权限
     */
    public function remove()
    {
        $id = input('post.id');
        $res = Db::name($this->table)->delete($id);
        if($res){
            $this->_success('删除系统权限成功');
        }else{
            $this->_error('删除系统权限失败');
        }
    }

    /**
     * 启用系统权限
     */
    public function resume()
    {
        $id = input('post.id');
        $updateData['status'] = 1;
        $res = Db::name($this->table)->where(['id'=>$id])->update($updateData);
        if($res){
            $this->_success('启用系统权限成功');
        }else{
            $this->_error('启用系统权限失败');
        }
    }


    /**
     * 禁用系统权限
     */
    public function forbid()
    {
        $id = input('post.id');
        $updateData['status'] = 0;
        $res = Db::name($this->table)->where(['id'=>$id])->update($updateData);
        if($res){
            $this->_success('禁用系统权限成功');
        }else{
            $this->_error('禁用系统权限失败');
        }

    }


}